limsTrainRecord.auditPage = {

    getGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.trainRecordGrid,
            url: _this.urlManager.trainRecordGrid + '/queries/tasks',
            columns: limsTrainRecord.getAuditListColumns(),
            onCellClick: function (field, row) {
                field === 'id' && _this.loadDetailPage(row.id)
            },
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    _this.refreshTabs(null)
                }
            },
            onRowActive: function (index, row) {
                _this.refreshTabs(row)
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.PASS',
                icon: 'pass',
                onClick: function () {
                    _this.pass()
                }
            }, {
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.REJECT',
                icon: 'reject',
                onClick: function () {
                    _this.reject()
                }
            }]
        }
    },

    refreshTabs: function (row) {
        let _this = this
        Gikam.getAsyncComp(_this.idManager.auditRecordGrid).done(function (comp) {
            if (row && row.id) {
                comp.refresh({
                    url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_TRAIN_RECORD&id=' + row.id
                })
            } else {
                comp.refresh({
                    url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_TRAIN_RECORD&id=' + 0
                })
            }
        })
        _this.refreshAttachments(_this.idManager.trainRecordGrid)
    },

    getAttachUploaderId: function (gridId) {
        return gridId + '-uploader'
    },

    refreshAttachments: function (gridId) {
        let _this = this
        let grid = Gikam.getComp(gridId)
        let actRow = grid.getActivedRow()
        Gikam.getAsyncComp(_this.getAttachUploaderId(gridId)).done(uploader => {
            if (actRow && actRow.id) {
                uploader.setOptions({
                    bizId: actRow.id,
                })
            } else {
                uploader.setOptions({
                    bizId: -1,
                })
            }
        })
    },

    getAttachmantStruct: function (gridId, dbTable, readonly) {
        let _this = this
        return {
            type: 'uploader',
            immediateUpload: true,
            id: _this.getAttachUploaderId(gridId),
            dbTable: dbTable,
            filter: false,
            genericQuery: false,
            readonly: readonly,
            onBeforeUpload: function () {
                var activedRow = Gikam.getComp(gridId).getActivedRow()
                if (Gikam.isEmpty(activedRow)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                    return false
                }
            },
        }
    },

    getAuditRecordGrid: function () {
        let _this = this
        return {
            type: 'grid',
            id: _this.idManager.auditRecordGrid,
            columns: limsTrainRecord.getFlowListColumns()
        }
    },

    getBaseInfoTab: function () {
        var _this = this
        return {
            type: 'tab',
            panels: [{
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [_this.getAttachmantStruct(_this.idManager.trainRecordGrid, 'T_LIMS_TRAIN_RECORD', true)]
            }, {
                title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
                items: [_this.getAuditRecordGrid()]
            }]
        }
    },

    idManager: {
        trainRecordGrid: 'lims-train-record-audit-list-grid',
        auditRecordGrid: 'lims-train-record-audit-list-grid' + '-record',
    },

    urlManager: {
        trainRecordGrid: limsTrainRecord.baseUrl,
    },

    pass: function () {
        let _this = this
        var grid = Gikam.getComp(_this.idManager.trainRecordGrid)
        Gikam.create('workflow').pass({
            data: grid.getSelections(),
            pageObject: limsTrainRecord,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            grid.refresh()
        })
    },

    reject: function () {
        let _this = this
        var grid = Gikam.getComp(_this.idManager.trainRecordGrid)
        Gikam.create('workflow').reject({
            data: grid.getSelections(),
            pageObject: limsTrainRecord,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            grid.refresh()
        })
    },

    loadDetailPage: function (id) {
        workspace.window.load(limsTrainRecord.baseUrl + '/page/detail' + Gikam.param({
            id: id
        }))
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-train-record-audit-list-layout',
            renderTo: workspace.window.$dom,
            north: {
                height: '55%',
                items: [this.getGridParam()]
            },
            center: {
                items: [this.getBaseInfoTab()]
            }
        })
    },

    init: function (param) {
        this.param = param
        this.create()
    }
}
